{% extends "dvaui/base.html" %}
{% load static from staticfiles %}
{% block page_css %}
{% endblock %}


{% block page_content %}
<section class="content-header" style="padding-top:0px" xmlns="http://www.w3.org/1999/html">
<div class="row">
    <div class="box box-info">
        <div class="box-header">
            <h2>List of Tasks, Operations & Queues</h2>
            <h3>{{ header }}</h3>
            {% if is_paginated %}
                <div class="pagination text-center" style="width:100%">
                    <span class="page-links">
                        {% if page_obj.has_previous %}
                            <a class="btn btn-info" style="float:left" href=".?page={{ page_obj.previous_page_number }}"><i class="fa fa-backward"></i>  Go back</a>
                        {% endif %}
                        <span class="page-current " style="font-size:24pt;font-weight:bold">
                            {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                        </span>
                        {% if page_obj.has_next %}
                            <a class="btn btn-info" style="float:right" href=".?page={{ page_obj.next_page_number }}">Load more  <i class="fa fa-forward"></i></a>
                        {% endif %}
                    </span>
                </div>
            {% endif %}
        </div>
    </div>
</div>
</section>
<section class="content">
<div class="row">
    <div class="col-lg-12">
        <div class="box">
            <div class="box-header with-border">
                <h3>Tasks</h3>
            </div>
            <div class="box-body">
                <form method="post">
                {% csrf_token %}
                <table class="table dataTables">
                    <thead>
                    <tr>
                        <th>Process</th>
                        <th>Group</th>
                        <th>Video</th>
                        <th>Operation</th>
                        <th>Queue</th>
                        <th>created</th>
                        <th>started</th>
                        <th>created ts</th>
                        <th>started ts</th>
                        <th>duration</th>
                        <th class="text-center">status</th>
                        <th class="text-center">view details</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for e in object_list %}
                        <tr>
                            <td><a href="/process/{{ e.parent_process_id }}">{{ e.parent_process_id }}</a></td>
                            <td>{{ e.task_group_id }}</td>
                            <td>{% if e.video %}<a href="/videos/{{ e.video_id }}">{{ e.video.name }}</a>{% endif %}</td>
                            <td>{{ e.operation }}</td>
                            <td>{{ e.queue }}</td>
                            <td class="text-center">{{ e.created|timesince}}</td>
                            <td class="text-center">{{ e.start_ts|timesince}}</td>
                            <td class="text-center">{{ e.created|date:"U" }}</td>
                            <td class="text-center">{{ e.start_ts|date:"U" }}</td>
                            <td class="text-right">{{ e.duration|floatformat }}</td>
                            <td>
                                {% if e.errored %}
                                    <a href="/task_detail/{{ e.pk }}" class="btn btn-danger btn-block">{% if e.imported %}Imported{% endif %} Failed</a>
                                {% elif e.duration < 0 and not e.started %}
                                    <a href="/task_detail/{{ e.pk }}" class="btn btn-warning btn-block">{% if e.imported %}Imported{% endif %} Pending</a>
                                {% elif e.duration < 0 and e.started and not e.completed%}
                                    <a href="/task_detail/{{ e.pk }}" class="btn btn-primary btn-block">{% if e.imported %}Imported{% endif %} Running</a>
                                {% elif e.completed%}
                                    <a href="/task_detail/{{ e.pk }}" class="btn btn-success btn-block">{% if e.imported %}Imported{% endif %} Completed</a>
                                {% else %}
                                    <a href="/task_detail/{{ e.pk }}" class="btn btn-info btn-block">Unknown</a>
                                {% endif %}
                            </td>
                            <td> <a href="/task_detail/{{ e.pk }}" class="btn btn-info btn-block">View details</a></td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
                </form>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        <div class="box">
            <div class="box-header with-border">
                <h4>Duration for completed tasks plotted against timestamp of start</h4>
            </div>
            <div class="box-body">
                <div id="start_taskplot"></div>
            </div>
        </div>
    </div>
    <div class="col-lg-12">
        <div class="box">
            <div class="box-header with-border">
                <h4>Duration for completed tasks plotted against timestamp of creation</h4>
            </div>
            <div class="box-body">
                <div id="created_taskplot"></div>
            </div>
        </div>
    </div>
</div>
</section>
{% endblock %}
{% block page_js%}
<script>
    $(document).ready(function() {
        InitializeTables();
        var start_plot_data = JSON.parse('{{ start_plot_data | escapejs }}');
        var created_plot_data = JSON.parse('{{ created_plot_data | escapejs }}');
        Plotly.newPlot('start_taskplot', start_plot_data);
        Plotly.newPlot('created_taskplot', created_plot_data);
        debugger;
    })
</script>
{% endblock %}
